<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>console.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_debug_console.js.html">console.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Simple logger that logs to the window console if available.
<a name="line17"></a> *
<a name="line18"></a> * Has an autoInstall option which can be put into initialization code, which
<a name="line19"></a> * will start logging if &quot;Debug=true&quot; is in document.location.href
<a name="line20"></a> *
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.debug.Console&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.debug.LogManager&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Logger.Level&#39;);
<a name="line27"></a>goog.require(&#39;goog.debug.TextFormatter&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Create and install a log handler that logs to window.console if available
<a name="line33"></a> * @constructor
<a name="line34"></a> */
<a name="line35"></a>goog.debug.Console = function() {
<a name="line36"></a>  this.publishHandler_ = goog.bind(this.addLogRecord, this);
<a name="line37"></a>
<a name="line38"></a>  /**
<a name="line39"></a>   * Formatter for formatted output.
<a name="line40"></a>   * @type {!goog.debug.TextFormatter}
<a name="line41"></a>   * @private
<a name="line42"></a>   */
<a name="line43"></a>  this.formatter_ = new goog.debug.TextFormatter();
<a name="line44"></a>  this.formatter_.showAbsoluteTime = false;
<a name="line45"></a>  this.formatter_.showExceptionText = false;
<a name="line46"></a>
<a name="line47"></a>  this.isCapturing_ = false;
<a name="line48"></a>  this.logBuffer_ = &#39;&#39;;
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * Loggers that we shouldn&#39;t output.
<a name="line52"></a>   * @type {!Object.&lt;boolean&gt;}
<a name="line53"></a>   * @private
<a name="line54"></a>   */
<a name="line55"></a>  this.filteredLoggers_ = {};
<a name="line56"></a>};
<a name="line57"></a>
<a name="line58"></a>
<a name="line59"></a>/**
<a name="line60"></a> * Returns the text formatter used by this console
<a name="line61"></a> * @return {!goog.debug.TextFormatter} The text formatter.
<a name="line62"></a> */
<a name="line63"></a>goog.debug.Console.prototype.getFormatter = function() {
<a name="line64"></a>  return this.formatter_;
<a name="line65"></a>};
<a name="line66"></a>
<a name="line67"></a>
<a name="line68"></a>/**
<a name="line69"></a> * Sets whether we are currently capturing logger output.
<a name="line70"></a> * @param {boolean} capturing Whether to capture logger output.
<a name="line71"></a> */
<a name="line72"></a>goog.debug.Console.prototype.setCapturing = function(capturing) {
<a name="line73"></a>  if (capturing == this.isCapturing_) {
<a name="line74"></a>    return;
<a name="line75"></a>  }
<a name="line76"></a>
<a name="line77"></a>  // attach or detach handler from the root logger
<a name="line78"></a>  var rootLogger = goog.debug.LogManager.getRoot();
<a name="line79"></a>  if (capturing) {
<a name="line80"></a>    rootLogger.addHandler(this.publishHandler_);
<a name="line81"></a>  } else {
<a name="line82"></a>    rootLogger.removeHandler(this.publishHandler_);
<a name="line83"></a>    this.logBuffer = &#39;&#39;;
<a name="line84"></a>  }
<a name="line85"></a>  this.isCapturing_ = capturing;
<a name="line86"></a>};
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Adds a log record.
<a name="line91"></a> * @param {goog.debug.LogRecord} logRecord The log entry.
<a name="line92"></a> */
<a name="line93"></a>goog.debug.Console.prototype.addLogRecord = function(logRecord) {
<a name="line94"></a>
<a name="line95"></a>  // Check to see if the log record is filtered or not.
<a name="line96"></a>  if (this.filteredLoggers_[logRecord.getLoggerName()]) {
<a name="line97"></a>    return;
<a name="line98"></a>  }
<a name="line99"></a>
<a name="line100"></a>  var record = this.formatter_.formatRecord(logRecord);
<a name="line101"></a>  var console = goog.debug.Console.console_;
<a name="line102"></a>  if (console) {
<a name="line103"></a>    switch (logRecord.getLevel()) {
<a name="line104"></a>      case goog.debug.Logger.Level.SHOUT:
<a name="line105"></a>        goog.debug.Console.logToConsole_(console, &#39;info&#39;, record);
<a name="line106"></a>        break;
<a name="line107"></a>      case goog.debug.Logger.Level.SEVERE:
<a name="line108"></a>        goog.debug.Console.logToConsole_(console, &#39;error&#39;, record);
<a name="line109"></a>        break;
<a name="line110"></a>      case goog.debug.Logger.Level.WARNING:
<a name="line111"></a>        goog.debug.Console.logToConsole_(console, &#39;warn&#39;, record);
<a name="line112"></a>        break;
<a name="line113"></a>      default:
<a name="line114"></a>        goog.debug.Console.logToConsole_(console, &#39;debug&#39;, record);
<a name="line115"></a>        break;
<a name="line116"></a>    }
<a name="line117"></a>  } else if (window.opera) {
<a name="line118"></a>    // window.opera.postError is considered an undefined property reference
<a name="line119"></a>    // by JSCompiler, so it has to be referenced using array notation instead.
<a name="line120"></a>    window.opera[&#39;postError&#39;](record);
<a name="line121"></a>  } else {
<a name="line122"></a>    this.logBuffer_ += record;
<a name="line123"></a>  }
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Adds a logger name to be filtered.
<a name="line129"></a> * @param {string} loggerName the logger name to add.
<a name="line130"></a> */
<a name="line131"></a>goog.debug.Console.prototype.addFilter = function(loggerName) {
<a name="line132"></a>  this.filteredLoggers_[loggerName] = true;
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * Removes a logger name to be filtered.
<a name="line138"></a> * @param {string} loggerName the logger name to remove.
<a name="line139"></a> */
<a name="line140"></a>goog.debug.Console.prototype.removeFilter = function(loggerName) {
<a name="line141"></a>  delete this.filteredLoggers_[loggerName];
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Global console logger instance
<a name="line147"></a> * @type {goog.debug.Console}
<a name="line148"></a> */
<a name="line149"></a>goog.debug.Console.instance = null;
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * The console to which to log.  This is a property so it can be mocked out in
<a name="line154"></a> * this unit test for goog.debug.Console.
<a name="line155"></a> * @type {Object}
<a name="line156"></a> * @private
<a name="line157"></a> */
<a name="line158"></a>goog.debug.Console.console_ = window.console;
<a name="line159"></a>
<a name="line160"></a>
<a name="line161"></a>/**
<a name="line162"></a> * Sets the console to which to log.
<a name="line163"></a> * @param {!Object} console The console to which to log.
<a name="line164"></a> */
<a name="line165"></a>goog.debug.Console.setConsole = function(console) {
<a name="line166"></a>  goog.debug.Console.console_ = console;
<a name="line167"></a>};
<a name="line168"></a>
<a name="line169"></a>
<a name="line170"></a>/**
<a name="line171"></a> * Install the console and start capturing if &quot;Debug=true&quot; is in the page URL
<a name="line172"></a> */
<a name="line173"></a>goog.debug.Console.autoInstall = function() {
<a name="line174"></a>  if (!goog.debug.Console.instance) {
<a name="line175"></a>    goog.debug.Console.instance = new goog.debug.Console();
<a name="line176"></a>  }
<a name="line177"></a>
<a name="line178"></a>  if (window.location.href.indexOf(&#39;Debug=true&#39;) != -1) {
<a name="line179"></a>    goog.debug.Console.instance.setCapturing(true);
<a name="line180"></a>  }
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Show an alert with all of the captured debug information.
<a name="line186"></a> * Information is only captured if console is not available
<a name="line187"></a> */
<a name="line188"></a>goog.debug.Console.show = function() {
<a name="line189"></a>  alert(goog.debug.Console.instance.logBuffer_);
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Logs the record to the console using the given function.  If the function is
<a name="line195"></a> * not available on the console object, the log function is used instead.
<a name="line196"></a> * @param {!Object} console The console object.
<a name="line197"></a> * @param {string} fnName The name of the function to use.
<a name="line198"></a> * @param {string} record The record to log.
<a name="line199"></a> * @private
<a name="line200"></a> */
<a name="line201"></a>goog.debug.Console.logToConsole_ = function(console, fnName, record) {
<a name="line202"></a>  if (console[fnName]) {
<a name="line203"></a>    console[fnName](record);
<a name="line204"></a>  } else {
<a name="line205"></a>    console.log(record);
<a name="line206"></a>  }
<a name="line207"></a>};
</pre>


</body>
</html>
